﻿using FineUI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.Article
{
    public partial class PartyArticleManageList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyArticleView";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        #region 数据加载
        private void LoadData()
        {
            // 权限检查
            CheckPowerWithButton("PartyArticleAdd", btnAdd);

            btnAdd.OnClientClick = Dialog.GetShowReference("PartyArticleManageEdit.aspx", "新增文章");

            // 每页记录数
            Grid1.PageSize = ConfigHelper.PageSize;

            BindGrid();

            //ppBranch.Value = this.CurrentUser.BranchID;
            ppBranch.Enabled = false;
            
        }

        private void BindGrid()
        {
            IQueryable<PartyArticle> q = DB.PartyArticles.Where(t => t.Module == "ThreeAffairOpen");

            // 在文章标题中搜索
            string title = txtTitle.Text.Trim();
            if (!string.IsNullOrEmpty(title))
            {
                q = q.Where(t => t.Title.Contains(title));
            }
            //if (!string.IsNullOrEmpty(ppBranch.Value))
            //{
            //    q = q.Where(t => t.BranchID == ppBranch.Value);
            //}
            if (!string.IsNullOrEmpty(this.CurrentUser.BranchID))
            {
                q = q.Where(t => t.BranchID == this.CurrentUser.BranchID);
            }
            /*
            if (!string.IsNullOrEmpty(ddlCategory.SelectedValue))
            {
                q = q.Where(t => t.Module == "ThreeAffairOpen" && t.Category == ddlCategory.SelectedValue);
            }
            */
            // 在查询添加之后，排序和分页之前获取总记录数
            Grid1.RecordCount = q.Count();

            // 排列和分页
            q = SortAndPage<PartyArticle>(q, Grid1);

            Grid1.DataSource = q;
            Grid1.DataBind();
        }

        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("PartyArticleEdit", Grid1, "editField");
            CheckPowerWithLinkButtonField("PartyArticleDelete", Grid1, "deleteField");
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string PartyArticleId = GetSelectedDataKeyID(Grid1);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("PartyArticleDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var del = DB.PartyArticles.FirstOrDefault(t => t.ID == PartyArticleId);
                DB.PartyArticles.Remove(del);
                DB.SaveChanges();
                BindGrid();
            }
        }

        public static string GetBranchName(string branchId)
        {
            return ControlBusiness.GetBranchPickerText(branchId);
        }

        public string GetCategory(string category)
        {
            string text = "";
            switch (category)
            {
                case "FinancePublic":
                    text = "财务公开";
                    break;
                case "PartyPublic":
                    text = "党务公开";
                    break;
                case "VillagePublic":
                    text = "村务公开";
                    break;
            }

            return text;
        }

        public string GetFlags(string flag)
        {
            string returnValue = string.Empty;
            switch (flag)
            {
                case "0":
                    returnValue = "无";
                    break;
                case "1":
                    returnValue = "置顶";
                    break;
                case "2":
                    returnValue = "固顶";
                    break;
                case "3":
                    returnValue = "置顶+固顶";
                    break;
            }
            return returnValue;
        }
        #endregion

        #region 按钮事件
        protected void ppBranch_TextChanged(object sender, EventArgs e)
        {
            ppBranch.ShowTrigger1 = !string.IsNullOrEmpty(ppBranch.Text);
        }

        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("PartyArticleDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            DB.PartyArticles.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyArticles.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            BindGrid();
        }

        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}